package cn.com.ibibj.alignmentService.service.impl;

import cn.com.ibibj.alignmentService.service.ApiCallerService;
import cn.hutool.http.HttpUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Mono;

import java.util.Map;

@Service
public class ApiCallerServiceImpl implements ApiCallerService {
    @Override
    public Mono<Boolean> call(String ipAndPart, String endpoint, Map<String, Object> params) {
        if (StringUtils.isEmpty(ipAndPart) || StringUtils.isEmpty(endpoint)) {
            return Mono.just(false);
        }
        String url = ipAndPart + endpoint;
        /**
         * 进行网络请求
         */
        return Mono.fromCallable(() -> {
                    try {
                        HttpUtil.post(url, params);
                        return true;
                    } catch (Exception e) {
                        try {
                            HttpUtil.get(url, params);
                            return true;
                        } catch (Exception e1) {
                            return false;
                        }
                    }
                })
                .onErrorReturn(false);
    }
}
